package com.sheliduan.itlearning.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sheliduan.itlearning.common.domain.dos.ArticleCourseDO;
import com.sheliduan.itlearning.common.domain.dos.CategoryDO;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author: 断舍离
 * @date: 2025/2/10 10:08
 * @description: 文章课程映射接口
 **/
public interface ArticleCourseMapper extends BaseMapper<ArticleCourseDO> {

    /*
     * 根据查询条件分页查询课程数据
     * */
    default Page<ArticleCourseDO> selectPageList(long current, long size, String title, Integer userId, LocalDateTime startDate, LocalDateTime endDate){
        // MyBatis分页对象, 需传入当前页码和每页记录数
        Page<ArticleCourseDO> page = new Page<>(current, size);
        LambdaQueryWrapper<ArticleCourseDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), ArticleCourseDO::getTitle, title)
                .eq(ArticleCourseDO::getUserId, userId)
                .ge(Objects.nonNull(startDate), ArticleCourseDO::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), ArticleCourseDO::getCreateTime, endDate) // 小于等于 endDate
                .orderByDesc(ArticleCourseDO::getCreateTime);
        return selectPage(page, wrapper);
    }

    /*
     * 根据查询条件分页查询课程数据(管理员用, 区别是不查看草稿状态的课程)
     * */
    default Page<ArticleCourseDO> selectPageListByAdmin(long current, long size, String title, LocalDateTime startDate, LocalDateTime endDate){
        // MyBatis分页对象, 需传入当前页码和每页记录数
        Page<ArticleCourseDO> page = new Page<>(current, size);
        LambdaQueryWrapper<ArticleCourseDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(title), ArticleCourseDO::getTitle, title)
                .ne(ArticleCourseDO::getStatus, "0")
                .ge(Objects.nonNull(startDate), ArticleCourseDO::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), ArticleCourseDO::getCreateTime, endDate) // 小于等于 endDate
                .orderByDesc(ArticleCourseDO::getCreateTime);
        return selectPage(page, wrapper);
    }

    /*
     * 修改课程
     * */
    default int updateCourse(ArticleCourseDO articleCourseDO) {
        LambdaUpdateWrapper<ArticleCourseDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ArticleCourseDO::getTitle, articleCourseDO.getTitle());
        wrapper.set(ArticleCourseDO::getCover, articleCourseDO.getCover());
        wrapper.set(ArticleCourseDO::getSummary, articleCourseDO.getSummary());
        wrapper.set(ArticleCourseDO::getContent, articleCourseDO.getContent());
        wrapper.set(ArticleCourseDO::getPrice, articleCourseDO.getPrice());
        wrapper.set(ArticleCourseDO::getStatus, articleCourseDO.getStatus());
        wrapper.set(ArticleCourseDO::getUpdateTime, articleCourseDO.getUpdateTime());
        wrapper.eq(ArticleCourseDO::getId, articleCourseDO.getId());
        return update(null, wrapper);
    }

    /*
    * 修改课程状态
    * */
    default int updateCourseStatus(Integer courseId, Integer status) {
        LambdaUpdateWrapper<ArticleCourseDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ArticleCourseDO::getStatus, status);
        wrapper.eq(ArticleCourseDO::getId, courseId);
        return update(null, wrapper);
    }
}
